package com.shinowit.dao;

import java.sql.Types;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.shinowit.model.TreeNode;

@Repository
public class OrgInfoDao {

	@Resource
	private JdbcTemplate jt;
	
	public TreeNode getOrgInfoTree(){
		TreeNode result=new TreeNode();
		List<Map<String,Object>> top_nodes= jt.queryForList("select * from sys_org_info where parent_org_code is null");
		result.setId("-1");
		result.setText("全部");
		for (Map<String,Object> row:top_nodes){
			TreeNode current_node=new TreeNode(row.get("org_code").toString(),row.get("org_name").toString());
			result.addChild(current_node);
			addChild(current_node);
		}
		return result;
	}
	
	public void addChild(TreeNode parentNode){
		List<Map<String,Object>> child_nodes= jt.queryForList("select org_code,org_name from sys_org_info where parent_org_code=?",new Object[]{parentNode.getId()},new int[]{Types.VARCHAR});
		for (Map<String,Object> row:child_nodes){
			TreeNode current_node=new TreeNode(row.get("org_code").toString(),row.get("org_name").toString());
			parentNode.addChild(current_node);
			addChild(current_node);
		}
		
	}
	
}
